<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2018\10\28 0028
 * Time: 10:03
 */
namespace app\admin\controller;


use app\admin\model\Commons;
use app\admin\model\Qinu;
use think\Controller;
use think\Db;
use think\Request;
use think\Validate;

class Adminuser extends Controller
{
//    public function __construct(Request $request)
//    {
//        Auths::login_auth($request);
//    }
    /**列表加载
     * @param Request $request
     * @return \think\response\View
     */
	public function show_list(Request $request){
	    $data = $this->page($request);
        $data['tru'] = false;
	    $adminuser = session('adminuser');
	    if($adminuser['role']==1){
            $data['tru'] = true;
        }
//	    echo '<pre>';print_r($adminuser);exit;
	    return view('user_list',$data);
    }

    /**分页
     * @param Request $request
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function page(Request $request){
	    $page = $request->param('page',1);
	    $limit = $request->param('limit',10);
	    $tiaojian = $request->param('tiaojian',null);
	    if($tiaojian){
            $data = Db::view('admin',true)
                ->view('rote','rolename','admin.role=rote.id','left')
                ->where('user_name|nick_name|phone|email|role','like',"%$tiaojian%")
                ->limit(($page-1)*$limit,$limit)
                ->select();
            $datarows = Db::view('admin',true)
                ->view('rote','rolename','admin.role=rote.id','left')
                ->where('user_name|nick_name|phone|email|role','like',"%$tiaojian%")
                ->count();
        }else{
	        $data = Db::view('admin',true)
                ->view('rote','rolename','admin.role=rote.id','left')
                ->limit(($page-1)*$limit,$limit)
                ->select();
	        $datarows = Db::view('admin',true)
                ->view('rote','rolename','admin.role=rote.id','left')
                ->count();
        }
        return ['data'=>$data,'datarows'=>$datarows,'tiaojian'=>$tiaojian];
    }

    /**加载添加页
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function show_add(){
        $rote = Db::name('rote')->select();
        return view('user_add',['data'=>$rote]);
    }

    /**添加
     * @return array|int|string
     */
    public function add(Request $request){
        $data = $request->post();
        $val = new Validate([
            'username'=>'require',
            'password'=>'require',
            'phone'=>'/^1[3456789][0-9]{9}$/',
            'email'=>'email',
            'password2'=>'require'
        ]);
        if(!$val->check($data)){
            return $val->getError();
        }
        if($data['password']!==$data['password2']){
            return '重复密码错误';
        }
        unset($data['password2']);
        //加密方式
        $data['password'] = Commons::getPassword($data['password']);
        //上传图片
//        return $data;
        $file = $request->file();
//        return $data;
        if(isset($file['avatar'])){
            $avatar = Qinu::Upfile($file['avatar']);
            if($avatar){
                $data['avatar'] = $avatar;
            }
        }
        $data['create_time'] = time();
        $data['status'] = 1;
        if(Db::name('admin')->insert($data)){
            return 1;
        }else{
            return '添加失败';
        }
    }

    /**加载编辑
     * @param Request $request
     * @return string|\think\response\View
     */
    public function edit(Request $request){
        $id = $request->param('id',null);
        if(!$id){
            return '请输入要修改的id';
        }
        $data = Db::name('admin')->where('id',$id)->find();
        $rote = Db::name('rote')->select();
        return view('edit',['data'=>$data,'rote'=>$rote]);
    }

    /**更新数据
     * @param Request $request
     * @return array|int|string
     */
    public function update(Request $request){
        $data = $request->post();
        if(!$data['id']){
            return '请输入要修改的id';
        }
        if($data['password']){
            $data['password'] = Commons::getPassword($data['password']);
        }else{
            unset($data['password']);
        }
//        return $data;
        $datas = Db::name('admin')->where('id',$data['id'])->find();
//        return $datas;
        $file = $request->file();
        if(isset($file['avatar'])){
            $avatar = Qinu::Upfile($file['avatar']);
            if($avatar){
//                return $avatar;
                if($datas['avatar']){
                    Qinu::DeleFile($datas['avatar']);
                }
                $data['avatar'] = $avatar;
            }
        }

        if(Db::name('admin')->where('id',$data['id'])->update($data)){
            return 1;
        }else{
            return '什么都没改变';
        }
    }

    /**删除接口
     * @param Request $request
     * @return int|string
     */
    public function del(Request $request){
        $id = $request->post('id',null);
        if(!$id){
            return '请输入你要删除的id';
        }
        $data = Db::name('admin')->where('id','in',$id)->select();
        if(Db::name('admin')->where('id','in',$id)->delete()){
            foreach ($data as $value){
                if($value['avatar']){
                    Qinu::DeleFile($value['avatar']);
                }
            }
            return 1;
        }else{
            return '删除失败';
        }
    }
    public function getuser(){
        $data = session('adminuser');
            $array = (Db::name('rote')->where('id',$data['role'])->find());
        $data['role'] = $array['rolename'];
//        echo '<pre>';
//        print_r($data);exit;
        return view('list',['data'=>$data]);
    }
}
